package com.lihepeng.leecode.dp;

import org.junit.Test;

/**
 * 使用动态规划完成费布那切数列
 * 状态转移方程为f(n) = f(n-1)+f(n-2)
 */
public class Fib {
    public Integer fib(int n) {
        if (n <= 2) {
            return n;
        }
        int[] memo = new int[n + 1];
        memo[1] = 1;
        memo[2] = 2;
        for (int i = 3; i <= n; i++) {
            memo[i] = memo[i - 2] + memo[i - 1];
        }
        return memo[n];
    }

    @Test
    public void runTest() {
        Integer fib = fib(3);
        System.out.println(fib);
    }

}
